package com.maoatao.easycodegen.toolkit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maoatao.easycodegen.framework.constant.Operator;
import com.maoatao.easycodegen.framework.constant.Order;
import com.maoatao.easycodegen.framework.model.BaseQuery;
import com.maoatao.easycodegen.framework.model.Expression;
import com.maoatao.easycodegen.framework.model.OrderItem;

import java.util.List;

public class QueryUtil {

    public static <T> QueryWrapper<T> toWrapper(BaseQuery<?, ?> query) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();

        // 处理表达式条件
        List<Expression> expressions = query.getExpressions();
        for (Expression expression : expressions) {
            String dbColumnName = expression.getColumnName().getDbColumnName();
            Operator operator = expression.getOperator();
            List<Object> values = expression.getValues();

            switch (operator) {
                case EqualTo:
                    if (isNotEmpty(values)) {
                        wrapper.eq(dbColumnName, values.get(0));
                    }
                    break;
                case NotEqualTo:
                    if (isNotEmpty(values)) {
                        wrapper.ne(dbColumnName, values.get(0));
                    }
                    break;
                case GreaterThan:
                    if (isNotEmpty(values)) {
                        wrapper.gt(dbColumnName, values.get(0));
                    }
                    break;
                case LessThan:
                    if (isNotEmpty(values)) {
                        wrapper.lt(dbColumnName, values.get(0));
                    }
                    break;
                case GreaterThanOrEqualTo:
                    if (isNotEmpty(values)) {
                        wrapper.ge(dbColumnName, values.get(0));
                    }
                    break;
                case LessThanOrEqualTo:
                    if (isNotEmpty(values)) {
                        wrapper.le(dbColumnName, values.get(0));
                    }
                    break;
                case In:
                    if (isNotEmpty(values)) {
                        wrapper.in(dbColumnName, values);
                    }
                    break;
                case NotIn:
                    if (isNotEmpty(values)) {
                        wrapper.notIn(dbColumnName, values);
                    }
                    break;
                case IsNull:
                    wrapper.isNull(dbColumnName);
                    break;
                case IsNotNull:
                    wrapper.isNotNull(dbColumnName);
                    break;
                case Between:
                    if (isNotEmpty(values) && values.size() >= 2) {
                        wrapper.between(dbColumnName, values.get(0), values.get(1));
                    }
                    break;
                case Like:
                    if (isNotEmpty(values)) {
                        wrapper.like(dbColumnName, values.get(0));
                    }
                    break;
                case NotLike:
                    if (isNotEmpty(values)) {
                        wrapper.notLike(dbColumnName, values.get(0));
                    }
                    break;
            }
        }

        // 处理排序条件
        List<OrderItem> orders = query.getOrders();
        for (OrderItem orderItem : orders) {
            String dbColumnName = orderItem.getColumnName().getDbColumnName();
            Order order = orderItem.getOrder();

            if (order == Order.ASC) {
                wrapper.orderByAsc(dbColumnName);
            } else if (order == Order.DESC) {
                wrapper.orderByDesc(dbColumnName);
            }
        }

        return wrapper;
    }

    /**
     * 判断对象是否非空
     *
     * @param obj 待判断的对象
     * @return 对象是否非空
     */
    private static boolean isNotEmpty(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof String) {
            return !((String) obj).isEmpty();
        }
        if (obj instanceof List) {
            return !((List<?>) obj).isEmpty();
        }
        return true;
    }
}
